/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.tools.particleeditor.i18n;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.StreamUtils;

public class I18N {

	private static Locale locale = Locale.CHINESE;
	private static Properties dictDefault = new Properties();
	private static Map<Locale, Properties> dictMap = new HashMap<Locale, Properties>();
	
	static {
		Locale[] locales = new Locale[] { null, Locale.CHINESE };
		for (Locale locale : locales) {
			String path = locale != null
							? "com/badlogic/gdx/tools/particleeditor/i18n/message_" + locale.getLanguage() + ".properties"
							: "com/badlogic/gdx/tools/particleeditor/i18n/message.properties";
			Reader reader = Gdx.files.internal(path).reader(10240, "utf-8");
			try {
				Properties prop = new Properties();
				prop.load(reader);
				if (locale == null)
					dictDefault = prop;
				else
					dictMap.put(locale, prop);
			} catch (IOException e) {
				throw new GdxRuntimeException(e);
			} finally {
				StreamUtils.closeQuietly(reader);
			}
		}

	}

	public static void setLocale(Locale locale) {
		I18N.locale = locale;
	}

	public static String getString(String key) {
		Properties prop = dictMap.get(locale);
		if (prop == null)
			prop = dictDefault;
		return prop.getProperty(key);
	}

}
